/*
 * @Author: yuki
 * @Date: 2025-10-26 16:29:52
 * @LastEditTime: 2025-10-26 17:15:59
 * @Description: jwt鉴权
 */

import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';

import type { JwtConfigType } from '@/config';

import { PrismaService } from './prisma.service';
import type { ITokenPayload } from './token.service';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(
    private config: ConfigService,
    private readonly prisma: PrismaService
  ) {
    const jwtConfig = config.get<JwtConfigType>('jwt')!;
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      secretOrKey: jwtConfig.accessSecret
    });
  }
  /**
   * 验证 JWT 令牌并返回有效载荷
   * @param playLoad 包含用户信息的有效载荷
   * @returns 验证后的有效载荷
   */
  validate(playLoad: ITokenPayload): ITokenPayload {
    return playLoad;
  }
}
